package com.bdqn.t382.Service;

import com.bdqn.t382.dao.SupplierMapper;
import com.bdqn.t382.entity.Supplier;

import java.util.List;

public class SupplierServiceImp implements SupplierService{
    private SupplierMapper supplierMapper;
    public void setSupplierMapper(SupplierMapper supplierMapper) {
        this.supplierMapper = supplierMapper;
    }

    @Override
    public List<Supplier> getAllSuppliers() {
        return supplierMapper.getAllSuppliers();
    }

    @Override
    public List<Supplier> getSuppliersByName(String name) {
        return supplierMapper.getSuppliersByName(name);
    }

    @Override
    public int saveBatch(List<Supplier> supplierList) {
        int rows = 0;
        for (int i = 0; i < supplierList.size(); i++) {
            // 拿出每一条用户数据插入
            Supplier supplier = supplierList.get(i);
            if(i == 1){
                // 插入第二条数据时，模拟数据库异常，查看事务是否生效
                throw new RuntimeException("数据库异常");
            }
            // 调用插入数据操作，计入总受影响行数
            rows += supplierMapper.insert(supplier);
        }
        // 返回总受影响行数
        return rows;
    }
}

